﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public enum Pivot 
    {
      Center,
      BottomLeft,
	  TopLeft,

	  Top,
	  TopRight,
      Left,

	  Right,
      Bottom,
      BottomRight, 
    }

    public struct RectSize   
    {
      public  float Top;
      public float Bottom;
      public float Left;
      public float Right;
    }

    public class UIElement :SGComponent
    {
        public Vector3 WorldPos
        {
            get { return this.transform.position; }
        }
        protected bool HasChanged = false;
        protected UIGeometry mGeom;
        public RectSize Padding ;
        public Color32 color = Color.white; 
        public virtual Vector4 GetElementSize() 
        {
            float Width = (float)this.PropertyValue<double>("Width");
            float Height = (float)this.PropertyValue<double>("Height"); ;
            Vector4 v = new Vector4();
            float w = Width + this.Padding.Left + this.Padding.Right;
            float h = Height + this.Padding.Top + this.Padding.Bottom;
            float halfwidth = w * 0.5f;
            float halfheight= h * 0.5f;
            Vector2 pv = this.GetPivotOffset();
            v.x = 0 - pv.x;
	        v.y = 0 - pv.y;
	        v.z = 1 - pv.x;
            v.w = 1 - pv.y;

            v.x *= Width;
            v.y *= Height;
            v.z *= Width;
            v.w *= Height;

            return v;
        }

        public Vector4 GetElemetPivotOffset() 
        {
            Vector2 pv = this.GetPivotOffset();
            Vector4 v = new Vector4();
            v.x = 0 - pv.x;
            v.y = 0 - pv.y;
            v.z = 1 - pv.x;
            v.w = 1 - pv.y;
            return pv;
        }

        public override void Init()
        {
            base.Init();
            this.AddProperty("X", 0d, this.Property_X_Set);
            this.AddProperty("Y", 0d, this.Property_Y_Set);
            this.AddProperty("Width", 0d, (n, o) => { HasChanged = true; });
            this.AddProperty("Height", 0d, (n, o) => { HasChanged = true; });
            this.AddProperty("Color", "#FFFFFF", (n, o) => { HasChanged = true; });
            this.AddProperty("Scale", 1d,Property_Scale_Set);
            this.AddProperty("Pivot", "Center", (n, o) => { HasChanged = true; });
            this.AddProperty("useCapture",false);
            this.AddProperty("OnClick","");
        }

        public override void OnEnter()
        {
            base.OnEnter();
            this.transform.localPosition = new Vector3((float)this.PropertyValue<double>("X"), (float)this.PropertyValue<double>("Y"),this.transform.localPosition.z - 0.1f);
            this.GetProperty("Scale").Value = this.GetProperty("Scale").Value;
        }
        public override void GenScript()
        {
            base.GenScript();
            string OnClickScriptStr =  (string)this.GetProperty("OnClick").Value;
            if (OnClickScriptStr != "") 
            {
                string fnName = "f"+System.Guid.NewGuid().ToString().Replace("-","");
                UIManager.JsEngine.Execute("var " + fnName + " = " + OnClickScriptStr);
                this.SetProperty("OnClick", fnName);
            }
        }
        public void Property_Scale_Set(object newValue, object oldValue) 
        {
            HasChanged = true;
            float scaleNumber = (float)(double)newValue;
            transform.localScale = new Vector3(scaleNumber, scaleNumber, scaleNumber);
        }
        public void Property_X_Set(object newValue, object oldValue) 
        {
            this.transform.localPosition = new Vector3((float)(double)newValue, this.transform.localPosition.y, this.transform.localPosition.z);     
        }

        public void Property_Y_Set(object newValue, object oldValue)
        {
            this.transform.localPosition = new Vector3(this.transform.localPosition.x, (float)(double)newValue, this.transform.localPosition.z);
        }
        
        public virtual void GenSelfMesh()
        { 

        }
        public virtual void UpdateToRender() 
        {
            MeshRenderer render = this.gameObject.GetComponent<MeshRenderer>();
            if (render != null)
            {
                MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();
                Mesh mesh = new Mesh();
                mesh.vertices = this.mGeom.verts.ToArray();
                mesh.colors = this.mGeom.cols.ToArray();
                mesh.SetIndices(new int[] { 0, 1, 2, 1, 3, 2 }, MeshTopology.Triangles, 0);
                mf.sharedMesh = mesh;
            }
        }
        public Vector2 GetPivotOffset() 
        {
            Vector2 v2 = new Vector2();
            string strPV =(string)this.GetProperty("Pivot").Value;
            Pivot pv = (Pivot)System.Enum.Parse(typeof(Pivot), strPV);
            if (pv == Pivot.Top || pv == Pivot.Center || pv == Pivot.Bottom)
            {
                v2.x = 0.5f;
            }
            else if (pv == Pivot.Right || pv == Pivot.TopRight || pv == Pivot.BottomRight)
            {
                v2.x = 1;
            }
            else 
            {
                v2.x = 0;
            }

            if (pv == Pivot.Left || pv == Pivot.Center || pv == Pivot.Right) 
            {
                v2.y = 0.5f;
            }
            else if (pv == Pivot.TopLeft || pv == Pivot.Top || pv == Pivot.TopRight)
            {
                v2.y = 1;
            }
            else 
            {
                v2.y = 0;
            }
            return v2;
        }
        void LateUpdate() 
        {
            if (HasChanged) 
            {
                this.GenSelfMesh();
                this.UpdateToRender();
                HasChanged = false;
            }
        }
    }
} 